//
//  ANProductSellerCell.m
//  Ants
//
//  Created by ZHM on 16/8/25.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANProductSellerCell.h"
#import <CoreLocation/CoreLocation.h>

@implementation ANProductSellerCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.thumbileImageView.contentMode = UIViewContentModeScaleAspectFill;
    //裁剪超出的
    self.thumbileImageView.clipsToBounds = YES;
}
#pragma mark- Actions
- (void)prepareForReuse {
    self.commentCountLabel.text = @"";
    self.locationLabel.text = @"未知";
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *kMerchantCellIdentifier = @"ANProductSellerCell";
    ANProductSellerCell *cell = [tableView dequeueReusableCellWithIdentifier:kMerchantCellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ANProductSellerCell" owner:nil options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}

- (void)setProduct:(ANBDProduct *)product {
    _product = product;
    self.locationLabel.text = [NSString stringWithFormat:@"%@-%@",_product.address[1],_product.address[2]];
    //NSString *thumbileURLString = [product.icons firstObject];
    NSDictionary *picDic = [product.icons firstObject];
    NSString *url = picDic[@"minPic"];
    UIImage *placeImage = [UIImage imageWithColor:UIColorFromRGB(0xE7EBEE)];
    
    [self.thumbileImageView sd_setImageWithURL: url.toURL placeholderImage:placeImage options:SDWebImageRetryFailed];
    
    self.nameLabel.text = product.title;
    NSDecimalNumber *price = product.price;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@元", price];
    //    if (self.product.commentCount) {
    //        self.commentCountLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%@人评论", nil), self.product.commentCount];
    //    }
    self.commentCountLabel.text = _product.createdAtStr;
}

- (void)reverseGeocodeLocation:(CLLocation *)location {
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    __weak typeof(self) weakSelf = self;
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count == 0 || error) {
            return;
        }
        CLPlacemark *placemark = placemarks.lastObject;
        weakSelf.locationLabel.text = [NSString stringWithFormat:@"%@-%@",placemark.locality,placemark.subLocality];
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
